package com.lt.flashcard.model.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * Tag information
 * @author ocsen
 * @version 1.0
 * @created 15-Aug-2012 17:02:58
 */
@Entity
@Table(name = "tag", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Tag implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Long id;
	private int reserved;
	private String name;
	private Set<FlashcardSet> flashcardsets = new HashSet<FlashcardSet>(0);
	private Set<Tag> child = new HashSet<Tag>(0);
	private Tag parent;

	public Tag() {
	}

	public Tag(int reserved, String name) {
		this.reserved = reserved;
		this.name = name;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "reserved", nullable = false)
	public int getReserved() {
		return this.reserved;
	}

	public void setReserved(int reserved) {
		this.reserved = reserved;
	}

	@Column(name = "name", unique = true, nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "flashcardset_has_tag", joinColumns = { @JoinColumn(name = "tag_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "flashcardSet_id", nullable = false, updatable = false) })
	public Set<FlashcardSet> getFlashcardsets() {
		return this.flashcardsets;
	}

	public void setFlashcardsets(Set<FlashcardSet> flashcardsets) {
		this.flashcardsets = flashcardsets;
	}

	@OneToMany(mappedBy="parent", fetch=FetchType.LAZY)
	public Set<Tag> getChild() {
		return child;
	}

	public void setChild(Set<Tag> child) {
		this.child = child;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "tag_id")
	public Tag getParent() {
		return parent;
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}
	
	@Override
	public String toString() {
		return this.name;
	}
}